package com.example.categorygroup.viewmodel;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.categorygroup.adapter.CategroyAdapter;
import com.example.categorygroup.adapter.CategroyValuesAdapter;
import com.example.categorygroup.data.CategoryValuesEntity;
import com.example.categorygroup.data.CategroyEntity;
import com.example.categorygroup.model.CategoryModel;
import com.example.mvvmcommon.mvvm.viewmodel.BaseViewModel;
import com.example.networkmoudel.entity.BaseEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CategroyViewModel extends BaseViewModel<CategoryModel> implements OnItemClickListener {

    private List<CategoryValuesEntity.DataBean> data=new ArrayList<>();
    private List<CategroyEntity> categroys=new ArrayList<>();
    public ObservableField<BaseQuickAdapter> categroyadapter=new ObservableField<>();
    public ObservableField<BaseQuickAdapter> valuesadapter=new ObservableField<>();
    public Integer linear=0;
    public Integer grid=1;


    @Override
    protected void initData() {
        Map<String,Object>map=new HashMap<>();
        map.put("parentId", 0);
        m.requestCategory(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
        categroys=m.requestCategroy();
        categroyadapter.set(new CategroyAdapter());
        valuesadapter.set(new CategroyValuesAdapter());
        categroyadapter.get().setOnItemClickListener(this);
        valuesadapter.get().setOnItemClickListener(this);
        categroyadapter.get().setNewInstance(categroys);
    }

    @Override
    protected CategoryModel createModel() {
        return new CategoryModel();
    }

    @Override
    protected void sucessFull(BaseEntity entity) {
        if (entity instanceof CategoryValuesEntity){
            CategoryValuesEntity entity1 = (CategoryValuesEntity) entity;
            data = entity1.getData();
            valuesadapter.get().setNewInstance(data);
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter instanceof CategroyAdapter){
            List<CategroyEntity> list=new ArrayList<>();
            for (int i = 0; i < categroys.size(); i++) {
                CategroyEntity categroyEntity = categroys.get(i);
                categroyEntity.selFlag=false;
                if (position==i){
                    categroyEntity.selFlag=true;
                }
                list.add(categroyEntity);
            }
            categroys = new ArrayList<>(list);
            categroyadapter.get().setNewInstance(categroys);
            Map<String,Object>map=new HashMap<>();
            map.put("parentId",categroys.get(position).parent_id);
            m.requestCategory(map).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(this);
        }else {

        }
    }
}
